﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectTest.Models
{
    public class QLBaiThi
    {
        private DBTestDataContext db = new DBTestDataContext();


        //lay toan bo thong tin bai thi bai thi
        public IEnumerable<BaiThi > GetAll()
        {
            return db.BaiThis;
        }
        //tim bai thi theo ma
        public BaiThi FindByCode(int MaBT)
        {
            return db.BaiThis.SingleOrDefault(p => p.MaBT==MaBT);
        }
        //Create
        public void Create(BaiThi bt)
        {
            try
            {
                db.BaiThis.InsertOnSubmit(bt);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
        //Edit
        public void Edit(BaiThi btEdited, int MBT)
        {
           BaiThi bt = db.BaiThis.SingleOrDefault(p => p.MaBT == MBT);
            try
            {
                UpdateInfo(bt, btEdited);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void UpdateInfo(BaiThi bt, BaiThi btEdited)
        {
            bt.ThoiGianThi = btEdited.ThoiGianThi;
            bt.MaDK = btEdited.MaDK;
            bt.Diem = btEdited.Diem;
            bt.XepLoai = btEdited.XepLoai;
            bt.MaCTBT = btEdited.MaCTBT;
        }
        //Delete
        public void Delete(int Mabt)
        {
           BaiThi bt = db.BaiThis.SingleOrDefault(p => p.MaBT== Mabt);
            try
            {
                db.BaiThis.DeleteOnSubmit(bt);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}